<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar @click-left="$router.back()" class="page-nav-bar" left-arrow title="个人信息" />
    <!-- /导航栏 -->

    <input @change="onFileChange" hidden ref="file" type="file" />

    <!-- 个人信息 -->
    <van-cell @click="$refs.file.click()" center class="photo-cell" is-link title="头像">
      <van-image :src="user.photo" class="avatar" fit="cover" round />
    </van-cell>
    <van-cell :value="user.name" @click="isnamePopup=true" is-link title="昵称"></van-cell>
    <van-cell
      :value="user.gender === 0 ? '男' : '女'"
      @click="isgenderPopup = true"
      is-link
      title="性别"
    ></van-cell>
    <van-cell :value="user.birthday" @click="isbirthdayPopup=true" is-link title="生日"></van-cell>
    <!-- /个人信息 -->

    <!-- 编辑昵称弹出层 -->
    <van-popup
      :style="{ height: '100%' }"
      class="namepopup"
      position="bottom"
      v-model="isnamePopup"
    >
      <update-name @close="isnamePopup=false" v-if="isnamePopup" v-model="user.name"></update-name>
    </van-popup>
    <!-- 性别弹出层-->
    <van-popup position="bottom" v-model="isgenderPopup">
      <update-gender @close="isgenderPopup=false" v-if="isgenderPopup" v-model="user.gender"></update-gender>
    </van-popup>

    <!-- 编辑生日 -->
    <van-popup position="bottom" v-model="isbirthdayPopup">
      <update-birthday
        @close="isbirthdayPopup=false"
        v-if="isbirthdayPopup"
        v-model="user.birthday"
      ></update-birthday>
    </van-popup>

    <!-- 编辑头像 -->

    <van-popup position="bottom" style="height:100%" v-model="isphotoPopup">
      <update-photo
        :img="img"
        @close="isphotoPopup=false"
        @update-photo="user.photo = $event"
        v-if="isphotoPopup"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserProfile',
  data () {
    return {
      user: {},
      isgenderPopup: false,
      isnamePopup: false,
      isbirthdayPopup: false,
      isphotoPopup: false,
      img: null
    }
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  created () {
    this.loadUserprofile()
  },
  methods: {
    async loadUserprofile () {
      try {
        const { data: res } = await getUserProfile()
        this.user = res.data
        console.log(this.user)
      } catch (err) {
        this.$toast.fail('加载失败，请重试')
      }
    },
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      console.dir(this.$refs.file);
      // 基于文章对象的获取blob 数据
      this.img = window.URL.createObjectURL(file)
      // console.log(data);
      // 展示弹出层
      this.isphotoPopup = true

      // file-input 如果选了同一个文件 不会触发change 事件
      // 解决方法就是每次使用完毕 把他的value清空
      this.$refs.file.value = ''
    }
  },
}
</script>

<style lang="less" scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .namepopup {
    background-color: #f5f7f9;
  }
  /deep/ .van-picker__cancel,
  /deep/ .van-picker__confirm {
    color: #1989fa !important;
  }
  .photo-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
  }
}
</style>